﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Data;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Windows.Forms.DataVisualization.Charting;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Globalization;

namespace nGSO.Tests
{
    [TestFixture]
    public class ChartRange
    {
        [Test]
        public void ChartJ2Ranges()
        {
            XDocument doc = XDocument.Parse(TestData.perf_all2);

            var entries = from e in doc.Descendants("PerfDataEntry")
                          select e;

            var result = entries.Select(s => new 
            {
                Population = int.Parse(s.Attribute("Population").Value),
                Iterations = int.Parse(s.Attribute("Iterations").Value),
                Efficiency = double.Parse(s.Attribute("Efficiency").Value, CultureInfo.InvariantCulture),
                Gso = s.Attribute("gso").Value,
                Name = s.Attribute("Name").Value
            }).ToList();

            foreach (var fun in result.GroupBy(s=>s.Name))
            {
                foreach (var funpop in fun.GroupBy(s=>s.Population))
                {
                    Chart chart = new Chart();
                    ChartArea chartArea1 = new ChartArea();
                    chartArea1.AxisX.Title = "Population";
                    chartArea1.AxisY.Title = "Normalized Efficiency";
                    string chartName = fun.Key + "_" + funpop.Key;

                    chart.Titles.Add(chartName);
                    chart.ChartAreas.Add(chartArea1);
                    chartArea1.AxisX.Minimum = 400;
                    chartArea1.AxisX.Interval = 200;
                    chartArea1.AxisX.Maximum = 1000;
                    chart.Legends.Add(new Legend() { });

                    foreach (var gsoType in funpop.GroupBy(s => s.Gso))
                    {
                        Series serie = chart.Series.Add(gsoType.Key);
                        serie.ChartType = SeriesChartType.FastPoint;
                        serie.MarkerSize = 12;

                        foreach (var grpIteration in gsoType.GroupBy(s => s.Iterations))
                        {
                            double miny = grpIteration.Min(s => s.Efficiency);
                            double maxy = grpIteration.Max(s => s.Efficiency);
                            double avgy = grpIteration.Average(s => s.Efficiency);

                            serie.Points.Add(new DataPoint(grpIteration.Key, new double[] { avgy }));
                        }    
                    }
                    chart.SaveImage(chartName + ".png", ImageFormat.Png);   
                }
            }

        }
    }
}
